const { answersModel, model } = require('../model/answers.js');

class Answers {
  /*
    添加
  */
  add(req, res) {
    const paper_id = req.body.paper_id; 
    if(req.cookies[`answer_${paper_id}`]){
      res.status(404)
      res.send('请勿重复作答');
      return;
    }
    answersModel.create(req.body).then((ret) => {
      res.cookie(`answer_${paper_id}`, '1', { expires: new Date(Date.now() + 1000*3600*24*7), httpOnly: true });
      res.status(200)
      res.send(ret)
    }).catch((ret) => {
      res.status(404)
      res.send(ret.errors[0].message)
    });
  }
}
module.exports = new Answers();